
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[LoginHostInsert]
    @UserId INT ,
    @Hostname VARBINARY(MAX) ,
    @Autologin BIT ,
    @Id INT OUTPUT
AS 
    BEGIN
    	
    	DECLARE @AutoLoginUsersCount INT 
        SELECT  @AutoLoginUsersCount = COUNT(*)
        FROM    dbo.LoginHost
        WHERE   LoginHost.Hostname = @Hostname
                AND LoginHost.Autologin = 1 
	
        IF ( @AutoLoginUsersCount > 0 ) 
            BEGIN
                UPDATE  dbo.LoginHost
                SET     LoginHost.Autologin = 0
                WHERE   LoginHost.Hostname = @Hostname 
            END
    	
        INSERT  INTO LoginHost
                ( UserId ,
                  Hostname ,
                  Autologin ,
                  DateCreated
                )
        VALUES  ( @UserId ,
                  @Hostname ,
                  @Autologin ,
                  GETDATE()
                )
	
        SET @Id = SCOPE_IDENTITY() ;
    END
GO
